package com.cos.ssm2.services.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.cos.ssm2.common.ResponseResult;
import com.cos.ssm2.dao.EmpDao;
import com.cos.ssm2.entity.Emp;
import com.cos.ssm2.entity.vo.EmpVo;
import com.cos.ssm2.services.EmpServices;
import com.cos.ssm2.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author: COS
 * @time: 2022/6/10 9:59
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class,timeout = 6000)
public class EmpServicesImpl implements EmpServices {
    @Autowired
    private EmpDao empDao;

    @Override
    public ResponseResult update(Emp emp) {
        if (emp != null) {
            if (empDao.updateEmp(emp)>0) {
                return ResponseResult.createSuccess();
            }
        }
        return ResponseResult.createFail("修改失败");
    }

    @Override
    public ResponseResult getEmpByEno(Integer eno) {
        if (eno>0) {
            Emp e = empDao.getEmpByEno(eno);
            return ResponseResult.createSuccess(e);
        }
        return  ResponseResult.createFail("没有数据傻爆了");
    }

    @Override
    public ResponseResult addEmp(Emp e) {
        if (e != null) {
            if (empDao.addEmp(e)>0) {
                return ResponseResult.createSuccess("ok啦");
            }
        }
        return ResponseResult.createFail("要死啦");
    }

    @Override
    public ResponseResult delEmp(Integer eno) {
        if (eno>0) {
            if (empDao.delEmp(eno)>0) {
                return ResponseResult.createSuccess();
            }
        }
        return ResponseResult.createFail("你太挫");
    }

    @Override
    public ResponseResult getEmpByPage(Integer page, Integer limit) {
        //使用分页工具
        PageHelper.startPage(page, limit);
        List<EmpVo> empList= empDao.getEmpWithDept();
        //封装数据到分页数据对象
        PageInfo<EmpVo> pi= new PageInfo<>(empList);
        if (empList!=null&&empList.size()>0) {
            return ResponseResult.createSuccess(pi);
        }
        return ResponseResult.createFail("出错了,请稍后重试!!!!!");
    }

    @Override
    public ResponseResult doLogin(String ename, String pwd) {
        if (StringUtils.isNotEmpty(ename,pwd)) {
            Emp emp = empDao.getEmpByEname(ename);
            if (emp != null&&emp.getPwd().equals(pwd)) {
                return ResponseResult.createSuccess(emp);
            }
        }
        return ResponseResult.createFail("登录失败,用户名或密码有误");
    }

    @Override
    public ResponseResult checkEname(String ename) {
        if (StringUtils.isNotEmpty(ename)) {
            //访问dao
            Emp e = empDao.getEmpByEname(ename);
            if (e != null) {
                return ResponseResult.createSuccess("用户名可用");
            }
        }
        return ResponseResult.createFail("找不到该用户名");
    }

    @Override
    public List<Emp> getAll() {
        return empDao.getAll();
    }
}
